//
//  GraphView.m
//  ZLChartBarTest
//
//  Created by 中矿科技有限公司 on 2017/11/10.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "GraphView.h"
#import "GraphBar.h"

#import "UIView+mySize.h"

@interface GraphView () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;


//X轴底部view
@property (strong, nonatomic) UIView *bottomView;


@end

@implementation GraphView

- (void)drawGraph
{
    self.width = [UIScreen mainScreen].bounds.size.width-30;
    self.height = 230;
    [self addSubview:self.scrollView];
    
    //获取数组中的最大值
    NSArray * sourtArray = [self.actualBarValues sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj2 floatValue] > [obj1 floatValue];
    }];
    //正向传值
    self.yMaxValue = [sourtArray.firstObject floatValue]*4/3;
    
    
    
    
    [self drawGrid];
   // [self drawLine];
    [self drawBar];

}






/**
 *  先画格子
 */

- (void)drawGrid
{
    [self drawXAxis];
   // [self drawYAxis];
}

/**
 *  画x坐标
 */

- (void)drawXAxis
{
    [self.bottomView removeFromSuperview];
    //底部
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height -GraphXBottomHeight, MainScreenWidth, GraphXBottomHeight)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    
    //分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0.5)];
    lineView.backgroundColor = HEXCOLOR(0x666666);
    [self.bottomView addSubview:lineView];
    
    [self.scrollView addSubview:self.bottomView];
    
    for (int i = 0; i < _xTitleArray.count; i++) {
        UILabel *axisLabel = [self createAxisLabelIsXaxis:YES];
        axisLabel.numberOfLines =0;
        axisLabel.textAlignment =NSTextAlignmentCenter;
        axisLabel.tag = BaseXAxisTag + i;
        axisLabel.text = [_xTitleArray objectAtIndex:i];
        axisLabel.left = _xScaleLength * i;
        [self.bottomView addSubview:axisLabel];
    }
}

- (UILabel *)createAxisLabelIsXaxis:(BOOL)xAxis
{
    
    CGFloat left = xAxis ? 0 : _xScaleLength;
    CGFloat width = xAxis ? _xScaleLength : MainScreenWidth - 50 - 10;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(left, 0, width, GraphXBottomHeight)];
    label.textColor = xAxis ? GraphXAxisFontColor : GraphYAxisFontColor;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:GraphFontSize];
    label.textAlignment = xAxis ? NSTextAlignmentCenter : NSTextAlignmentRight;
    return label;
}

/**
 *  画柱状图
 */
- (void)drawBar
{
    //CGFloat GraphBarWidthAndGap =12;
    //CGFloat targetX = (self.xScaleLength - 3*GraphBarWidthAndGap) / 2;
    //CGFloat actualX = targetX + GraphBarWidthAndGap * 2;
    
    for (UIView *view in _scrollView.subviews) {
        if ([view isKindOfClass:[GraphBar class]]) {
            [view removeFromSuperview];
        }
    }
    
    for (int i = 0; i < self.actualBarValues.count; i++) {
        GraphBar *actualBar = [[GraphBar alloc] initWithFrame:CGRectMake(self.xScaleLength * i, 0, _xScaleLength, self.height - GraphXBottomHeight-1)];
        actualBar.barColor = [UIColor colorFromHex:@"4CB5FF"];
        CGFloat actualValue = [[self.actualBarValues objectAtIndex:i] floatValue] / self.yMaxValue;
        actualBar.currentBarValue =[[self.actualBarValues objectAtIndex:i] floatValue];
        actualBar.barValue = actualValue;
        
        [_scrollView addSubview:actualBar];
    }
}



- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.minimumZoomScale = 0.5;
        _scrollView.maximumZoomScale = 2;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(_xScaleLength * _xTitleArray.count, self.height);
    }
    return _scrollView;
}


@end
